<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true">

        <!--<el-form-item>-->
          <!--<span>状态：</span>-->
          <!--<el-radio-group v-model="selectOptions.status" @change="currentStatusChange">-->
            <!--<el-radio-button v-for="(item, index) in statusMaps" :label="item.value" name="status" :key="index">{{ item.label }}-->
            <!--</el-radio-button>-->
          <!--</el-radio-group>-->
        <!--</el-form-item>-->

        <!--<el-form-item>-->
          <!--<span>身份：</span>-->
          <!--<el-radio-group v-model="selectOptions.farm_status" @change="currentStatusChange">-->
            <!--<el-radio-button v-for="(item, index) in cardMaps" :label="item.value" name="farm_status" :key="index">{{ item.label }}-->
            <!--</el-radio-button>-->
          <!--</el-radio-group>-->
        <!--</el-form-item>-->
        <div style="display: inline-block;float: right">
          <el-select v-model="selectOptions.query" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
          <el-form-item>
            <el-input :placeholder="selectOptions.query" v-model="selectOptions.keywords" @keyup.enter.native="doSearch"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" v-on:click="doSearch">查询</el-button>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" style="background: #13ce66" v-on:click="_reload">刷新</el-button>
          </el-form-item>
        </div>
      </el-form>

    </el-col>

    <!--列表-->
    <el-table :data="users" highlight-current-row v-loading="listLoading"
              style="width: 100%;" element-loading-text="玩命请求中。。。">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column label="uid" width="85" prop="uid">
      </el-table-column>
      <el-table-column label="用户信息" width="200">
        <template slot-scope="scope">
          <div class="people">
            <span class="name">{{ scope.row.nickname }}</span>
            <!--<br >-->
            <span class="name">{{ scope.row.phone }}</span>
          </div>
          <div class="user-avatar" v-if="scope.row.avatar">
            <a :href="scope.row.avatar" target="_blank"><img :src="scope.row.avatar" alt=""></a>
          </div>
          <div class="user-avatar" v-else>
            <img src="../../../static/images/default.png" alt="">
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="sex" label="性别" width="100" :formatter="formatSex" sortable>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="80" :formatter="userStatus">
      </el-table-column>
      <el-table-column prop="custom_field_name" label="地址信息" min-width="180">
        <template slot-scope="props">
          <div v-if="props.row.address">
            <span>{{ props.row.address.province }}{{ props.row.address.city }}{{ props.row.address.district }}{{ props.row.address.edit }}</span>
            <p>收货人：{{ props.row.address.name }};  手机号：{{ props.row.address.phone }};  性别：{{ props.row.address.sex === '1' ? '男' : props.row.address.sex === '2' ? '女' : '未知' }}</p>
          </div>
          <span v-else>无地址信息</span>
        </template>
      </el-table-column>
      <el-table-column prop="farm_status" label="是否默认" width="120" :formatter="setDefault">
      </el-table-column>
      <el-table-column prop="create_time" label="地址创建时间" width="240" :formatter="createAddressTime">
      </el-table-column>
      <el-table-column prop="farm_status" label="用户身份" width="120" :formatter="userPosition">
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-pagination
        @current-change="currentPageChange"
        :page-size="30"
        layout="prev, pager, next, jumper"
        :total="total" style="float:right;" :current-page="parseInt(selectOptions.page)">
      </el-pagination>
      <span style="float: right;margin-top: 6px;margin-right: 5px">共 {{total}} 条数据</span>
    </el-col>

  </section>
</template>

<script type="text/ecmascript-6">
  import util from '../../util'
  import NProgress from 'nprogress'
	import { apiTree } from '../../apiTree'

  export default {
    data() {
      return {
        options: [
          {value: 'uid', label: '用户uid'},
          {value: 'phone', label: '手机号'},
          {value: 'nickname', label: '昵称'}
        ],
        statusMaps: [
          {value: -1, label: '全部'},
          {value: 1, label: '正常'},
          {value: 0, label: '锁定'}
        ],
        cardMaps: [
          {value: -1, label: '所有'},
          {value: 1, label: '农场主'},
          {value: 0, label: '普通用户'}
        ],
        users: [],
        total: 0,
        listLoading: false,
        selectOptions: {
          page: this.$route.query.page !== undefined ? this.$route.query.page : 1,
          query: this.$route.query.query !== undefined ? this.$route.query.query : '',
          keywords: this.$route.query.keywords !== undefined ? this.$route.query.keywords : '',
          status: this.$route.query.status !== undefined ? this.$route.query.status : -1,
          farm_status: this.$route.query.farm_status !== undefined ? this.$route.query.farm_status : -1
        }
      }
    },
    methods: {
      // 显示是否为默认
      setDefault(row) {
        return row.is_default === '0' ? '否' : row.is_default === '1' ? '是' : '无'
      },
      // 性别显示转换
      formatSex: function (row, column) {
        return row.sex === '1' ? '男' : row.sex === '2' ? '女' : '未知'
      },
      _reload() {
        this.getUsersAddress()
      },
      userPosition(row, column) {
        return row.farm_status === '1' ? '农场主' : '普通用户'
      },
      // 状态转换
      userStatus(row, column) {
        return row.status === '0' ? '锁定' : '正常'
      },
      // 创建时间
      createAddressTime(row, column) {
        if (row.create_time) {
          return util.formatDate.format(new Date(row.create_time * 1000), 'yyyy-MM-dd hh:mm:ss')
        } else {
          return '用户没有填写地址'
        }
      },
      // 改变当前状态
      currentStatusChange() {
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      currentPageChange(val) {
        this.selectOptions.page = val
        this.$router.push({path: this.$route.path, query: this.selectOptions})
      },
      doSearch() {
        if (this.selectOptions.query && this.selectOptions.keywords) {
          this.$router.push({path: this.$route.path, query: this.selectOptions})
        }
      },
      // 获取用户地址列表
      getUsersAddress() {
        let para = this.$route.query
        if (util.isEmptyObject(para)) {
          this.selectOptions = {
            page: 1,
            query: '',
            keywords: '',
            status: -1,
            farm_status: -1
          }
        }
        this.listLoading = true
        NProgress.start()
         apiTree('user/address', 'user/address',para).then((res) => {
          this.users = res.d.data
          this.total = res.d.total_num
          this.listLoading = false
          NProgress.done()
        })
      }
    },
    created() {
      this.getUsersAddress()
    },
    watch: {
      $route: 'getUsersAddress'
    }
  }

</script>

<style scoped lang="stylus" rel="stylesheet/stylus">
  .user-avatar
    width 60px
    height 60px
    border-radius 50%
    & img
      width 100%
      height 100%
      border-radius 50%
  .cell {
    overflow: hidden;
  }
  .name {
    display: block;
    padding-top: 10px;
    display: block;
    width: 100px;
    white-space: nowrap;
    text-overflow: ellipsis;
    overflow: hidden;
  }
  .people {
    float: right;
  }
  .user-avatar {
    float: left;
  }
</style>
